package com.demo.util;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.util.Properties;

import com.alibaba.druid.pool.DruidDataSource;

public class CollectionOracle {
	private static DruidDataSource dataSource = null;
	static {
		/**
		 * 数据库代码连接是固定的，无法修改
		 * 
		 * 因此我们需要将数据库连接信息提取出来,读取properties
		 */
		try {
			Properties properties = new Properties();
			properties.load(new FileInputStream("src/main/resources/jdbc.properties"));
			
			dataSource = new DruidDataSource();
			
			dataSource.setDriverClassName(properties.getProperty("jdbc.driverClassName"));
			dataSource.setUrl(properties.getProperty("jdbc.url"));
			dataSource.setUsername(properties.getProperty("jdbc.userName"));
			dataSource.setPassword(properties.getProperty("jdbc.password"));
			
			dataSource.setInitialSize(Integer.parseInt(properties.getProperty("jdbc.init")));
			dataSource.setMaxActive(Integer.parseInt(properties.getProperty("jdbc.max")));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static Connection getConnection(boolean isAutiCommit) throws Exception {
		Connection connection = dataSource.getConnection();
		
		connection.setAutoCommit(isAutiCommit);
		return connection;
	}
	public static Connection getConnection() throws Exception {
		return getConnection(false);
	}
	
	
//	private static Connection connection = null;
//	
//	static {
//		try {
//			Class.forName("oracle.jdbc.OracleDriver");
//			connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "lx", "lx");
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//	}
//	
//	public static Connection getConnection(boolean isAutiCommit) throws Exception {
//		connection.setAutoCommit(isAutiCommit);
//		return connection;
//	}
//	public static Connection getConnection() throws Exception {
//		return getConnection(false);
//	}
}
